﻿using System;
using System.Web.Routing;

namespace BitworkStudios.Web.Mvc.Framework.Navigation
{
    public class UrlContext
    {
        public string Action { get; set; }
        public string Controller { get; set; }
        public string Url { get; set; }

        public UrlContext(string url)
        {
            Url = url;
            var uri = new Uri(RouteInfo.ToAbsoluteUrl(Url));
            var routeInfo = new RouteInfo(uri, RouteInfo.CurrentApplicationPath);

            RouteData routeData = routeInfo.RouteData;
            Controller = routeData.Values["controller"].ToString();
            Action = routeData.Values["action"].ToString();
        }

        public UrlContext(string action, string controller)
        {
            Action = action;
            Controller = controller;

            if (!string.IsNullOrEmpty(action) && !string.IsNullOrEmpty(controller))
            {
                Url = RouteInfo.Url.Action(Action, Controller);
            }
        }
    }
}
